package cn.iocoder.yudao.module.shop.service.manager;

import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.manager.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.manager.ShopManagerDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.manager.ShopManagerConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.manager.ShopManagerMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 店铺管理员 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopManagerServiceImpl implements ShopManagerService {

    @Resource
    private ShopManagerMapper managerMapper;

    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createManager(ShopManagerCreateReqVO createReqVO) {
        // 插入
        ShopManagerDO existedManager = getManagerByUserId(createReqVO.getUserId());
        if(existedManager != null){
            throw exception(MANAGER_HAD_EXISTS);
        }

        AdminUserRespDTO adminUserRespDTO = adminUserApi.getUser(createReqVO.getUserId());
        if(adminUserRespDTO == null){
            throw exception(MANAGER_NOT_FIND_USER);
        }

        ShopManagerDO manager = ShopManagerConvert.INSTANCE.convert(createReqVO);
        manager.setMobile(adminUserRespDTO.getMobile());

        managerMapper.insert(manager);
        // 返回
        return manager.getId();
    }

    @Override
    public void updateManager(ShopManagerUpdateReqVO updateReqVO) {
        // 校验存在
        validateManagerExists(updateReqVO.getId());
        // 更新
        ShopManagerDO updateObj = ShopManagerConvert.INSTANCE.convert(updateReqVO);
        managerMapper.updateById(updateObj);
    }

    @Override
    public void deleteManager(Long id) {
        // 校验存在
        validateManagerExists(id);
        // 删除
        managerMapper.deleteById(id);
    }

    private void validateManagerExists(Long id) {
        if (managerMapper.selectById(id) == null) {
            throw exception(MANAGER_NOT_EXISTS);
        }
    }

    @Override
    public ShopManagerDO getManager(Long id) {
        return managerMapper.selectById(id);
    }

    public ShopManagerDO getManagerByUserId(Long userId){
        return managerMapper.selectOne(ShopManagerDO::getUserId, userId);
    }

    @Override
    public List<ShopManagerDO> getManagerList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return managerMapper.selectBatchIds(ids);
    }

    @Override
    public List<ShopManagerDO> getManagerListByUserIds(Collection<Long> ids){
        if(CollUtil.isEmpty(ids)){
            return ListUtil.empty();
        }

        return managerMapper.selectList(ShopManagerDO::getUserId, ids);
    }

    @Override
    public PageResult<ShopManagerDO> getManagerPage(ShopManagerPageReqVO pageReqVO) {
        return managerMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopManagerDO> getManagerList(ShopManagerExportReqVO exportReqVO) {
        return managerMapper.selectList(exportReqVO);
    }

}
